import os
from openai import OpenAI

"""
DeepSeek API调用示例

使用前请确保：
1. 已安装OpenAI SDK: pip3 install openai
2. 已设置DEEPSEEK_API_KEY环境变量
"""

# 创建OpenAI客户端实例，配置为使用DeepSeek API
client = OpenAI(
    api_key=os.environ.get('DEEPSEEK_API_KEY'),  # 从环境变量获取API密钥
    base_url="https://api.deepseek.com")

try:
    # 调用DeepSeek的chat模型
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": "Hello"},
        ],
        stream=False
    )
    
    # 输出模型的回复
    print("模型回复:")
    print(response.choices[0].message.content)
    
except Exception as e:
    print(f"发生错误: {e}")
    print("请确保：")
    print("1. 已安装OpenAI SDK: pip3 install openai")
    print("2. 已正确设置DEEPSEEK_API_KEY环境变量")
    print("3. 网络连接正常")